package com.huisheng.mapper;

import com.huisheng.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;

/**
 * @Authther: mrwu
 * @date:2021/3/5-03
 * @Description:com.huisheng.mapper
 */
@CacheNamespace(implementation = RedisCache.class)
public interface UserMapper {
    //mapper 配置开发
    public List<User> findAll();
    public List<User> findAllUserandRole();
    //注解开发
    //插入用户
    @Insert("insert into user values(#{id},#{name},#{total})")
    public void addUser(User user);
    //查询用户
    @Select("select * from user")
    public List<User> selectUser();
    //删除 某个条件的用户
    @Delete("delete from user where id = #{id}")
    public void deleteUser(int id);
    //通过id 查询
    @Select("select * from user where id = #{id}")
    public User findById(int id);

    @Select("select * from user")
    @Results({
            @Result(id = true, property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "total",column = "total"),
            @Result(property = "oders",column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.huisheng.mapper.OrderMapper.findOrderByUid"))
    })
    public List<User> findAllUserandOrder();

    //获取所有用户 及他所有的角色
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "total",column = "total"),
            @Result(property = "roles",column = "id",many = @Many(select = "com.huisheng.mapper.RoleMapper.findRolesByUid")),
    })
    public List<User> findAllUserAndRole();

}
